//
//  HCStarSelectedButton.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/21.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCStarSelectedButton.h"

@interface HCStarSelectedButton () {
    NSMutableArray *buttonsArray;
    NSMutableArray *buttonsTitle;
    NSMutableArray *buttons;
}

@end

@implementation HCStarSelectedButton

-(instancetype)initWithFrame:(CGRect)frame withScore:(CGFloat)score {
    if (self = [super initWithFrame:frame]) {
        self.score = score;
        [self initializeSubView:frame];
    }
    return self;
}

-(void)initializeSubView:(CGRect)frame {
    buttonsArray = [NSMutableArray array];
    if (self.score<1) {
        buttonsTitle = [NSMutableArray arrayWithObjects:@"卫生不合格",@"服务态度差",@"质量不好",@"店铺乱",@"售后差",@"设施与描述不符", nil];
    }else{
        buttonsTitle = [NSMutableArray arrayWithObjects:@"卫生合格",@"服务态度好",@"质量好",@"店铺干净",@"售后不错",@"星级店铺", nil];
        
    }
    buttons = [NSMutableArray array];
    
    for (int i = 0; i<6; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i+1;
        button.frame = CGRectMake((i%2)*(YBKAdaptiveW(15) + (frame.size.width - YBKAdaptiveW(15))/2.0), (i/2)*(YBKAdaptiveW(15) + (frame.size.height - 2.0*YBKAdaptiveW(15))/3.0), (frame.size.width - YBKAdaptiveW(15))/2.0, (frame.size.height - 2*YBKAdaptiveW(15))/3.0);
        button.layer.cornerRadius = 3.0f;
        button.layer.borderWidth = 1.0f;
        [button setTitle:buttonsTitle[i] forState:UIControlStateNormal];
        button.titleLabel.font = YBKSystemFont(YBKAdaptiveFont(12));
        [button setTitleColor:YBKRGBCOLOR(196, 196, 196, 1) forState:UIControlStateNormal];
        button.layer.borderColor = YBKRGBCOLOR(204, 204, 204, 1).CGColor;
        
        [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        [buttons addObject:button];
        
    }
    
}

-(void)btnAction:(UIButton *)btn {
    if ([buttonsArray containsObject:btn]) {
        [buttonsArray removeObject:btn];
        btn.layer.borderColor = YBKRGBCOLOR(204, 204, 204, 1).CGColor;
        [btn setTitleColor:YBKRGBCOLOR(196, 196, 196, 1) forState:UIControlStateNormal];
    }else{
        [buttonsArray addObject:btn];
        btn.layer.borderColor = YBKRGBCOLOR(240, 96, 96, 1).CGColor;
        [btn setTitleColor:YBKRGBCOLOR(240, 96, 96, 1) forState:UIControlStateNormal];
    }

    if ([self.delegate respondsToSelector:@selector(starSelectedButtonClicked:)]&&self.delegate) {
        [self.delegate starSelectedButtonClicked:buttonsArray];
    }
    
}

-(void)reloadDataWith:(CGFloat)score {
    if (score == 1.0f) {
        buttonsTitle = [NSMutableArray arrayWithObjects:@"卫生合格",@"服务态度好",@"质量好",@"店铺干净",@"售后不错",@"星级店铺", nil];
        
        //清空数据也
        [buttonsArray removeAllObjects];
        
        for (int i = 0; i<buttons.count; i++) {
            UIButton *btn = buttons[i];
            [btn setTitle:buttonsTitle[i] forState:UIControlStateNormal];
            btn.layer.borderColor = YBKRGBCOLOR(204, 204, 204, 1).CGColor;
            [btn setTitleColor:YBKRGBCOLOR(196, 196, 196, 1) forState:UIControlStateNormal];
        }
        
    }else{
        NSLog(@"选中的分数:%f",score);
        buttonsTitle = [NSMutableArray arrayWithObjects:@"卫生不合格",@"服务态度差",@"质量不好",@"店铺乱",@"售后差",@"设施与描述不符", nil];
        
        //清空数据也
        [buttonsArray removeAllObjects];
        
        for (int i = 0; i<buttons.count; i++) {
            UIButton *btn = buttons[i];
            [btn setTitle:buttonsTitle[i] forState:UIControlStateNormal];
            btn.layer.borderColor = YBKRGBCOLOR(204, 204, 204, 1).CGColor;
            [btn setTitleColor:YBKRGBCOLOR(196, 196, 196, 1) forState:UIControlStateNormal];
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
